package edu.infomatik.tundra.components;

import java.util.Locale;

import org.apache.tapestry5.SelectModel;
import org.apache.tapestry5.ValueEncoder;
import org.apache.tapestry5.annotations.Parameter;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.annotations.SetupRender;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.services.PersistentLocale;

import edu.infomatik.tundra.model.LocaleEncoder;
import edu.infomatik.tundra.model.LocaleSelectModel;

public class LocaleSwitcher {
	
    @Parameter(defaultPrefix = "literal", required = true)
	private String supportedLocales;

	@Inject
	private PersistentLocale persistentLocale;
	
	@SessionState
	private Locale selectedLocale;
		
	public Locale getSelectedLocale() {
		selectedLocale=persistentLocale.get();
		return selectedLocale; 
	}
	
	@SetupRender 	
	void onStartLocale(){
		selectedLocale=persistentLocale.get();
	}
	
	public void setSelectedLocale(Locale selectedLocale) {
		persistentLocale.set(selectedLocale);
	}

	public SelectModel getLocaleModel(){
		return new LocaleSelectModel(supportedLocales);
	}
	public ValueEncoder<Object> getLocaleEncoder() {
		return new LocaleEncoder();
	}
}